using System;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

namespace Avanade.PolicyInjection
{
    /// <summary>
    /// An attribute that applies the <see cref="WcfValidationCallHandler"/> to its target.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property)]
    public class WcfValidationCallHandlerAttribute : HandlerAttribute
    {
        private string ruleSet;
        private SpecificationSource specificationSource;

        /// <summary>
        /// Constructs a new <see cref="WcfValidationCallHandlerAttribute"/> that uses the
        /// default ruleset.
        /// </summary>
        public WcfValidationCallHandlerAttribute()
            : this(string.Empty)
        {
        }

        /// <summary>
        /// Constructs a new <see cref="WcfValidationCallHandlerAttribute"/> that uses the
        /// given ruleset.
        /// </summary>
        /// <param name="ruleSet">Ruleset name to use.</param>
        public WcfValidationCallHandlerAttribute(string ruleSet)
        {
            this.ruleSet = ruleSet;
            this.specificationSource = SpecificationSource.Both;
        }

        /// <summary>
        /// <see cref="SpecificationSource"/> determining where to get validation rules.
        /// </summary>
        /// <value>specification source.</value>
        public SpecificationSource SpecificationSource
        {
            get { return specificationSource; }
            set { specificationSource = value; }
        }

        /// <summary>
        /// Derived classes implement this method. When called, it
        /// creates a new call handler as specified in the attribute
        /// configuration.
        /// </summary>
        /// <returns>A new call handler object.</returns>
        public override ICallHandler CreateHandler()
        {
            return new WcfValidationCallHandler(ruleSet, specificationSource);
        }
    }
}
